package com.demo.java.reqprint;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 用来测发送的报文的
 */
public class SimpleServer {

    public static void main(String [] args) {
        try {
            ServerSocket serverSocket = new ServerSocket(6666);
            serverSocket.setSoTimeout(60*1000);//60s

            Socket server = serverSocket.accept();
            System.out.println("远程主机地址：" + server.getRemoteSocketAddress());

            InputStream ins = server.getInputStream();

            int n;
            byte[] byteArr = new byte[1024];
            List<Byte> byteList = new LinkedList<>();
            while ((n = ins.read(byteArr)) != -1) { // 读取到缓冲区
                for(int i=0;i<n;i++){
                    byteList.add(byteArr[i]);
                }
                if(n<1024){
                    break;
                }
            }
            byteArr = new byte[byteList.size()];
            for(int i=0;i<byteArr.length;i++){
                byteArr[i]=byteList.get(i);
            }

            String s = new String(byteArr, StandardCharsets.UTF_8);

            System.out.println(s);

            server.close();
        }catch(IOException e) {
            e.printStackTrace();
        }
    }

}
